﻿using CommonTools;
using Hardcodet.Wpf.TaskbarNotification;
using log4net;
using Microsoft.Win32;
using SimWithP3d.Vehicle;
using SimWithP3d.ViewModel;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;

namespace SimWithP3d
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        public static bool debug = false;
        public static RealDataViewModel realDataViewModel;
        private TaskbarIcon notifyIcon;
        App()
        {
            if (CommonTools.CommonTool.CheckSameProcedure("SimWithP3d"))
            {
                Environment.Exit(0);
            }
        }
        public static ILog log = LogManager.GetLogger("Logger");
        public static void RecordErrorLog(Exception ex)
        {
            log.Info("在" + ex.StackTrace.Split('在').Last() + ex.Message);
        }
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);

            //create the notifyicon (it's a resource declared in NotifyIconResources.xaml
            notifyIcon = (TaskbarIcon)FindResource("NotifyIcon");
            initVehicleDevice();
            BootStart bs = new BootStart();
            bs.SetMeAutoStart(true);
        }

        protected override void OnExit(ExitEventArgs e)
        {
            notifyIcon.Dispose(); //the icon would clean up automatically, but this is cleaner
            base.OnExit(e);
        }
        #region UDP实例,通信相关
        /// <summary>
        /// 视景主机EP
        /// </summary>
        public static IPEndPoint LocalUdpEp;
        /// <summary>
        /// 教员站ep
        /// </summary>
        public static IPEndPoint remoteEP;
        /// <summary>
        /// UDP实例
        /// </summary>
        public static UdpClient udpClient;
        /// <summary>
        /// 表示UdpClient对其它客户端的连接不限制
        /// </summary>
        public static IPEndPoint temp = null;
        #region 下位机驱动 
        /// <summary>
        /// 相应飞机型号下位机驱动
        /// </summary>
        public static VehicleDevice vehicleDevice = null;
        /// <summary>
        /// 根据配置文件初始化下位机驱动型号
        /// </summary>
        private void initVehicleDevice()
        {
            string aircraftType = ConfigurationManager.AppSettings["aircraftType"];
            if (vehicleDevice == null)
            {
                vehicleDevice = new Pa28();
            }
        }
        #endregion
        #region 发送消息方法 重载 1.string 2.byte[]
        public static void UDPSendMessage(string sendmessage)
        {
            try
            {
                byte[] sendmess = Encoding.UTF8.GetBytes(sendmessage);
                udpClient.Send(sendmess, sendmess.Length, remoteEP);
            }
            catch (Exception e)
            {
                Console.WriteLine("发送失败！" + e.ToString());
            }
        }

        public static void UDPSendMessage(byte[] sendmessage)
        {
            try
            {

                udpClient.Send(sendmessage, sendmessage.Length, remoteEP);
            }
            catch (Exception e)
            {
                Console.WriteLine("发送失败！" + e.ToString());
            }
        }
        #endregion
        #endregion
    }
}
